﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;

namespace ForkCarRelationManager
{
    public partial class ClientManageform : Form
    {
        public ClientManageform()
        {
            InitializeComponent();
        }

        private void ClientManageform_Load(object sender, EventArgs e)
        {

            this.initData();
        }

        private void initData()
        {
            dbOperate oprate = new dbOperate();
            //查找所有分公司作为数据源
            String sqlCompany = "select s.ID,s.ScompanyName from ScompanyInfor as s";
            DataTable companyTable = oprate.GetDate(sqlCompany);
            this.TBCompany.DataSource = companyTable;
            this.TBCompany.DisplayMember = "ScompanyName";
            this.TBCompany.ValueMember = "ScompanyName";

            //初始化数据列表
            String SQL = "SELECT (select sum(1) from ClientInfor where ID<=client.ID) as ID,client.ClientName,iif(client.ClientType = 1,'购买客户',iif(client.ClientType = 2,'租赁客户',iif(client.ClientType = 0,'维修客户','其他'))) as type,client.Location,company.ScompanyName,client.ContactName,client.ContactPosition,client.ContactPhone,client.LoadTime,client.SalesmanName,client.ID as clentId From  ClientInfor as client left join ScompanyInfor as company on client.ScompanyId = company.Id";
            DataTable dt = oprate.GetDate(SQL);
            this.dataGridView1.DataSource = dt;
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            String sql = "SELECT ( select sum(1) from ClientInfor where ID<=client.ID) as ID,client.ClientName,iif(client.ClientType = 1,'购买客户',iif(client.ClientType = 2,'租赁客户',iif(client.ClientType = 3,'维修客户','其他'))) as type,client.Location,company.ScompanyName,client.ContactName,client.ContactPosition,client.ContactPhone,client.LoadTime,client.SalesmanName,client.ID as clentId From  ClientInfor as client left join ScompanyInfor as company on client.ScompanyId = company.Id where 1=1";
            //按照客户匹配查找
            if (this.TBClientName.Text.Length != 0)
            {  
                //client.ClientName like '*严*' and client.Locationlike '*城阳*' and company.ScompanyName = '田园牧歌'";
                sql = sql + " and client.ClientName like '%" + this.TBClientName.Text + "%'";
            }
            //按照区域查找
            if (this.TBLocation.Text.Length != 0)
            {
                //client.ClientName like '*严*' and client.Locationlike '*城阳*' and company.ScompanyName = '田园牧歌'";
                sql = sql + " and client.Location like '%" + this.TBClientName.Text + "%'";
            }
             //按照公司查找
            if (this.TBCompany.Text.Length != 0)
            {
                //client.ClientName like '*严*' and client.Locationlike '*城阳*' and company.ScompanyName = '田园牧歌'";
                sql = sql + " and company.ScompanyName = '" + this.TBCompany.Text + "'";
            }
            //按照录入时间查找
            if(this.dateTimePicker1.Value<=this.dateTimePicker2.Value)
            {
                sql = sql + " and LoadTime between #" + this.dateTimePicker1.Value + "# and #" + this.dateTimePicker2.Value+"#";
            }
            dbOperate oprate = new dbOperate();
            DataTable dt = oprate.GetDate(sql);
            this.dataGridView1.DataSource = dt;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            FormAddClient formClient = new FormAddClient();
            formClient.ShowDialog();
            if (formClient.DialogResult == DialogResult.Yes)
            {
                this.initData();  
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "查看")
            {
                String id = dataGridView1.CurrentRow.Cells[(dataGridView1.CurrentRow.Cells.Count) - 1].Value.ToString();
                MessageBox.Show(id);
            }
                
        }

        private void TBCompany_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void labelX1_Click(object sender, EventArgs e)
        {

        }

        private void labelX2_Click(object sender, EventArgs e)
        {

        }

        private void TBLocation_TextChanged(object sender, EventArgs e)
        {

        }

        private void labelX3_Click(object sender, EventArgs e)
        {

        }

        private void labelX4_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void labelX7_Click(object sender, EventArgs e)
        {

        }
    }
}
